<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\common\model\Authrule as AuthruleModel;
use Data;


class Authrule extends Common
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $treeDatas = AuthruleModel::getRuleTree();
        return view('', compact('treeDatas'));
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        return view();
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $data = input();
        $result = $this->validate($data, 'Authrule');
        if (true !== $result) {
            // 验证失败 输出错误信息
            $this->error($result);
        }
        //添加数据
        $AuthruleM = new AuthruleModel;
        $AuthruleM->title = $data['title'];
        $AuthruleM->pid = $data['pid'];
        $AuthruleM->name = strtolower($data['name']);
        if ($AuthruleM->save()) {
            $this->success('添加成功', '/admin/authrule');
        } else {
            $this->error('操作有误');
        };
    }

    /*一键添加权限规则*/
    public function onekeycreate()
    {
        return view();
    }

    /*保存一键添加权限规则*/
    public function onekeysave()
    {
        $data = input();
        $title = $data['title']; //权限名
        $name = strtolower($data['name']) . '/'; //规则
        $AuthruleM = new AuthruleModel;
        $AuthruleM->title = $title . '首页';
        $AuthruleM->name = $name . 'index';
        $AuthruleM->save();
        // 获取自增ID
        $pid = $AuthruleM->id;
        //定义控制器的方法映射
        $ruleArrs = [
            $name . 'create' => $title . '创建显示',
            $name . 'save' => $title . '创建保存',
            $name . 'edit' => $title . '编辑显示',
            $name . 'update' => $title . '编辑保存',
            $name . 'delete' => $title . '删除'
        ];
        $ruleData = [];
        foreach ($ruleArrs as $key => $ruleArr) {
            $ruleData[] = [
                'pid' => $pid,
                'title' => $ruleArr,
                'name' => $key
            ];
        }
        $AuthruleM = new AuthruleModel;
        if ($AuthruleM->saveAll($ruleData)) {
            $this->success('操作成功', '/admin/authrule');
        } else {
            $this->error('操作有误');
        };
    }

    /**
     * 显示指定的资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $ruleData = AuthruleModel::find($id);
        return view('', compact('ruleData'));
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request $request
     * @param  int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $data = input();
        $result = $this->validate($data, 'Authrule.edit');
        if (true !== $result) {
            // 验证失败 输出错误信息
            $this->error($result);
        }
        $AuthruleM = AuthruleModel::get($id);
        $AuthruleM->title = $data['title'];
        $AuthruleM->name = $data['name'];
        if ($AuthruleM->save()) {
            $this->success('操作成功', '/admin/authrule');
        } else {
            $this->error('编辑失败');
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //检测是否有子权限
        if (AuthruleModel::where('pid', $id)->find()) {
            $res['status'] = '0';
            $res['msg'] = '请先删除子权限';
            return $res;
        }

        //删除数据
        if (AuthruleModel::where('id', $id)->delete()) {
            $res['status'] = '1';
            $res['msg'] = '删除成功';
        } else {
            $res['status'] = '0';
            $res['msg'] = '删除失败';
        }
        return $res;
    }
}
